package com.atguigu.chapter11;

import com.atguigu.chapter05.WaterSensor;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

import java.time.Duration;

import static org.apache.flink.table.api.Expressions.$;

/**
 * TODO
 *
 * @author cjp
 * @version 1.0
 * @date 2021/3/12 9:30
 */
public class Flink18_SQL_GroupWindow_EventTime {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        SingleOutputStreamOperator<WaterSensor> sensorDS = env
                .socketTextStream("localhost", 9999)
                .map(new MapFunction<String, WaterSensor>() {
                    @Override
                    public WaterSensor map(String value) throws Exception {
                        // 切分
                        String[] line = value.split(",");
                        return new WaterSensor(line[0], Long.parseLong(line[1]), Integer.parseInt(line[2]));

                    }
                })
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                .<WaterSensor>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                                .withTimestampAssigner((value, ts) -> value.getTs() * 1000L)
                );


        // TODO - SQL — GroupWindow（基于时间或条数的  滚动、滑动、会话 窗口）
        // 1.创建 表的执行环境
        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);
        // 2.将 流 转换成 Table
        tableEnv.createTemporaryView("sensor", sensorDS, $("id"), $("ts"), $("vc"), $("et").rowtime());
        // 3.使用 SQL 进行 GroupWindow 开窗
        tableEnv
                // 基于时间的 滚动窗口
//                .sqlQuery("select " +
//                        "id," +
//                        "sum(vc)," +
//                        "tumble_start(et,interval '5' second) as windowStart," +
//                        "tumble_end(et,interval '5' second) as windowEnd " +
//                        "from sensor " +
//                        "group by id,tumble(et,interval '5' second)")
                // 基于时间的 滑动窗口
//                .sqlQuery("select " +
//                        "id," +
//                        "sum(vc)," +
//                        "hop_start(et,interval '2' second,interval '5' second) as windowStart," +
//                        "hop_end(et,interval '2' second,interval '5' second) as windowEnd " +
//                        "from sensor " +
//                        "group by id,hop(et,interval '2' second,interval '5' second)")
                // 基于时间的 会话窗口
                .sqlQuery("select " +
                        "id," +
                        "sum(vc)," +
                        "session_start(et,interval '3' second) as windowStart," +
                        "session_end(et,interval '3' second) as windowEnd " +
                        "from sensor " +
                        "group by id,session(et,interval '3' second)")
                .execute()
                .print();


        env.execute();
    }
}

/**
 */
